package net.zzy.pbbot.plugin.todo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.lz1998.pbbot.utils.Msg;
import net.zzy.pbbot.plugin.todo.entity.QqDaoEntity;
import net.zzy.pbbot.plugin.todo.entity.TodoDaoEntity;
import net.zzy.pbbot.plugin.todo.mapper.QqMapper;
import net.zzy.pbbot.plugin.todo.mapper.TodoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhuZhaoYang
 * @date 2021/11/29 13:27
 */
@Component
public class TodoService {

    private TodoMapper todoMapper;
    private QqMapper qqMapper;

    @Autowired
    public void setQqMapper(QqMapper qqMapper) {
        this.qqMapper = qqMapper;
    }

    @Autowired
    public void setTodoMapper(TodoMapper todoMapper) {
        this.todoMapper = todoMapper;
    }

    public Msg addTodo(Long qqNumber, String content) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qqNumber));
        if (number == null) {
            number = new QqDaoEntity();
            number.setNumber(qqNumber);
            qqMapper.insert(number);
        }
        TodoDaoEntity todoDaoEntity = new TodoDaoEntity();
        todoDaoEntity.setContent(content);
        todoDaoEntity.setQqId(number.getId());
        todoDaoEntity.setState(0);
        todoMapper.insert(todoDaoEntity);
        return Msg.builder().text("添加待办成功");
    }

    public Msg finishTodo(Long qqNumber, Integer todoId) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qqNumber));
        if (number == null) {
            return Msg.builder().text("你还未添加任何待办");
        }
        TodoDaoEntity todoDaoEntity = todoMapper.selectOne(new QueryWrapper<TodoDaoEntity>()
                .eq("qq_id", number.getId())
                .eq("id", todoId));
        if (todoDaoEntity == null) {
            return Msg.builder().text("待办不存在");
        }
        todoDaoEntity.setState(1);
        todoMapper.updateById(todoDaoEntity);
        return Msg.builder().text("已完成待办");
    }

    public Msg getTodo(Long qqNumber) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qqNumber));
        if (number == null) {
            return Msg.builder().text("你还未添加任何待办");
        }
        List<TodoDaoEntity> todoDaoEntities = todoMapper.selectList(new QueryWrapper<TodoDaoEntity>()
                .eq("qq_id", number.getId())
                .eq("state", 0));
        if (todoDaoEntities.size() == 0) {
            return Msg.builder().text("无未完成的待办");
        }
        Msg msg = Msg.builder();
        msg.text("======未完成待办======\n");
        for (TodoDaoEntity todoDaoEntity : todoDaoEntities) {
            msg.text(todoDaoEntity.getId() + ". " + todoDaoEntity.getContent() + "\n");
        }
        msg.text("======总计" + todoDaoEntities.size() + "项待办======");
        return msg;
    }

    public Msg getFinishedTodo(Long qqNumber) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qqNumber));
        if (number == null) {
            return Msg.builder().text("你还未添加任何待办");
        }
        List<TodoDaoEntity> todoDaoEntities = todoMapper.selectList(new QueryWrapper<TodoDaoEntity>()
                .eq("qq_id", number.getId())
                .eq("state", 1));
        if (todoDaoEntities.size() == 0) {
            return Msg.builder().text("无已完成的待办");
        }
        Msg msg = Msg.builder();
        msg.text("======已完成待办======\n");
        for (TodoDaoEntity todoDaoEntity : todoDaoEntities) {
            msg.text(todoDaoEntity.getId() + ". " + todoDaoEntity.getContent() + "\n");
        }
        return msg;
    }

    public Msg modTodo(Long qqNumber, Integer todoId, String content) {
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", qqNumber));
        if (number == null) {
            return Msg.builder().text("你还未添加任何待办");
        }
        TodoDaoEntity todoDaoEntity = todoMapper.selectOne(new QueryWrapper<TodoDaoEntity>()
                .eq("qq_id", number.getId())
                .eq("id", todoId));
        if (todoDaoEntity == null) {
            return Msg.builder().text("待办不存在");
        }
        todoDaoEntity.setContent(content);
        todoMapper.updateById(todoDaoEntity);
        return Msg.builder().text("修改成功");
    }

    public Msg copyTodo(Long userId, Long sourceId) {
        if (userId.equals(sourceId)) {
            return Msg.builder().text("不能复制自己的待办");
        }
        QqDaoEntity number = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", sourceId));
        if (number == null) {
            return Msg.builder().text("该qq号未添加任何待办");
        }
        List<TodoDaoEntity> todoDaoEntities = todoMapper.selectList(new QueryWrapper<TodoDaoEntity>()
                .eq("qq_id", number.getId())
                .eq("state", 0));
        if (todoDaoEntities.size() == 0) {
            return Msg.builder().text("该qq号无未完成待办");
        }
        QqDaoEntity user = qqMapper.selectOne(new QueryWrapper<QqDaoEntity>().eq("number", userId));
        if (user == null) {
            user = new QqDaoEntity();
            user.setNumber(userId);
            qqMapper.insert(user);
        }
        for (TodoDaoEntity todoDaoEntity : todoDaoEntities) {
            todoDaoEntity.setId(null);
            todoDaoEntity.setQqId(user.getId());
            todoMapper.insert(todoDaoEntity);
        }
        return Msg.builder().text("复制成功");
    }
}
